package com.aidex.biz.service.impl;

import com.aidex.biz.domain.PushRule;
import com.aidex.biz.domain.PushRuleUser;
import com.aidex.biz.mapper.PushRuleUserMapper;
import com.aidex.biz.messageSend.MessageSendService;
import com.aidex.biz.service.PushRuleUserService;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.utils.NumberUtils;
import com.aidex.common.utils.StringUtils;
import com.aidex.framework.cache.ConfigUtils;
import com.aidex.system.service.CompanyService;
import com.aidex.system.service.ISysUserService;
import com.aidex.system.service.SysDeptService;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 规则用户对应Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-03-11
 */
@Service
@Transactional(readOnly = true)
public class PushRuleUserServiceImpl extends BaseServiceImpl<PushRuleUserMapper, PushRuleUser> implements PushRuleUserService {
    private static final Logger log = LoggerFactory.getLogger(PushRuleUserServiceImpl.class);
    /**
     * 企业微信消息
     */
    @Autowired
    MessageSendService messageSendService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 获取单条数据
     *
     * @param pushRuleUser 规则用户对应
     * @return 规则用户对应
     */
    @Override
    public PushRuleUser get(PushRuleUser pushRuleUser) {
        PushRuleUser dto = super.get(pushRuleUser);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 规则用户对应id
     * @return 规则用户对应
     */
    @Override
    public PushRuleUser get(String id) {
        PushRuleUser dto = super.get(id);
        return dto;
    }


    /**
     * 查询规则用户对应列表
     *
     * @param pushRuleUser 规则用户对应
     * @return 规则用户对应
     */
    @Override
    public List<PushRuleUser> findList(PushRuleUser pushRuleUser) {
        return super.findList(pushRuleUser);
    }

    /**
     * 分页查询规则用户对应列表
     *
     * @param pushRuleUser 规则用户对应
     * @return 规则用户对应
     */
    @Override
    public PageInfo<PushRuleUser> findPage(PushRuleUser pushRuleUser) {
        return super.findPage(pushRuleUser);
    }

    /**
     * 保存规则用户对应
     *
     * @param pushRuleUser
     * @return 结果
     */
    @Override
    public boolean save(PushRuleUser pushRuleUser) {
        return super.save(pushRuleUser);
    }


    /**
     * 删除规则用户对应信息
     *
     * @param pushRuleUser
     * @return 结果
     */
    @Override
    public boolean remove(PushRuleUser pushRuleUser) {
        return super.remove(pushRuleUser);
    }

    /**
     * 批量删除规则用户对应
     *
     * @param ids 需要删除的规则用户对应ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deletePushRuleUserByIds(String[] ids) {
        return mapper.deletePushRuleUserByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param pushRuleUser 规则用户对应
     * @return 结果
     */
    @Override
    public int findMaxSort(PushRuleUser pushRuleUser) {
        return NumberUtils.nextOrder(mapper.findMaxSort(pushRuleUser));
    }

    @Override
    public List<PushRuleUser> getListByUserIds(String[] userIds) {
        List<PushRuleUser> list = new ArrayList<>();
        PushRuleUser pushRuleUser = null;
        for (String id : userIds) {
            pushRuleUser = new PushRuleUser();
            SysUser sysUser = userService.get(id);
            pushRuleUser.setUserId(sysUser.getUserId());
            pushRuleUser.setUserName(sysUser.getName());
            pushRuleUser.setCompanyId(sysUser.getCompanyId());
            pushRuleUser.setCompanyName(companyService.get(sysUser.getCompanyId()).getCompanyName());
            pushRuleUser.setDeptId(sysUser.getDeptId());
            pushRuleUser.setDeptName(sysDeptService.get(sysUser.getDeptId()).getDeptName());
            pushRuleUser.setIsEnable("1");

            list.add(pushRuleUser);
        }
        return list;
    }

    @Override
    public List<PushRuleUser> findUserJoinRuleList(PushRuleUser pushRuleUser) {
        return mapper.findUserJoinRuleList(pushRuleUser);
    }

    @Transactional(readOnly = false)
    @Override
    public int deletePushRuleUserByRuleIds(String[] ids) {
        return mapper.deletePushRuleUserByRuleIds(ids);
    }

    @Override
    public List<PushRuleUser> findShareUserByRule(PushRule pushRule) {
        return mapper.findShareUserByRule(pushRule);
    }

    @Override
    public List<PushRuleUser> findQyjlInRuleUser(PushRuleUser pushRuleUser) {
        return mapper.findQyjlInRuleUser(pushRuleUser);
    }

    @Override
    @Transactional(readOnly = false)
    public int resetDayShareCount() {
        return mapper.resetDayShareCount();
    }

    @Override
    @Transactional(readOnly = false)
    public int changeWorkingPushRuleUser(PushRuleUser pushRuleUser) {
        int result = mapper.changeWorkingPushRuleUser(pushRuleUser);
        // 异步执行
        CompletableFuture.runAsync(() -> {
            try {
                // 微信通知经理
                String sendSmsIsSend = ConfigUtils.getConfigValueByKey("sendWxms.isSend");
                if (StringUtils.isNotNull(sendSmsIsSend) && "是".equals(sendSmsIsSend)) {
                    //1.发送消息给个人
                    SysUser sysUser = userService.get(pushRuleUser.getUserId());
                    SysDept sysdept = sysDeptService.get(pushRuleUser.getDeptId());
                    String deptParentIds = sysdept.getParentIds();
                    if (StringUtils.isNotNull(deptParentIds)) {
                        String[] deptParentIdsArr = deptParentIds.split("/");
                        if (deptParentIdsArr.length > 1) {
                            String qyjlDeptId = deptParentIdsArr[1];
                            List<PushRuleUser> qyjlList = mapper.findQyjlByDeptId(qyjlDeptId);
                            for (PushRuleUser qyjl : qyjlList) {
                                if (StringUtils.isNotNull(qyjl) && !qyjl.getUserId().equals(pushRuleUser.getUserId())) {
                                    String workingStatus = "0".equals(pushRuleUser.getIsEnable()) ? "停止" : "开启";
                                    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
                                    String msg = sysUser.getName() + "-" + workingStatus + "了接单";
                                    String content = msg + "(" + sdf.format(new Date()) + ")";
                                    messageSendService.sendTextMessageByUser(new String[]{qyjl.getUserId()}, content);
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
            }
        });
        return result;
    }

    @Override
    public List<PushRuleUser> findAllShareUserByRule(PushRule pushRule) {
        return mapper.findAllShareUserByRule(pushRule);
    }

    @Override
    public List<PushRuleUser> findQyjlByCity(String city) {
        return mapper.findQyjlByCity(city);
    }

    @Override
    public List<PushRuleUser> findPushUserListByRuleIds(List<PushRule> pushRules) {
        return mapper.findPushUserListByRuleIds(pushRules);
    }

    @Override
    public int resetRuleUserTotalCount() {
        return mapper.resetRuleUserTotalCount();
    }
}
